package paint;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

public class PaintContainer extends PaintComponent {
	protected List<PaintComponent> components = new ArrayList<PaintComponent>();
	protected Color outlineColor;
	protected Color backgroundColor;
	protected int widthOffset = 0;

	public PaintContainer(Color outline, Color background, Rectangle bounds) {
		super(bounds);

		outlineColor = outline;
		backgroundColor = background;
	}

	public PaintContainer() {
		super();
	}

	public void setWidthOffset(int widthOffset) {
		this.widthOffset = widthOffset;
	}

	public void add(PaintComponent component) {
		components.add(component);

		component.bounds.x += bounds.x;
		component.bounds.y += bounds.y;
	}

	public void remove(PaintComponent component) {
		components.remove(component);

		component.bounds.x -= bounds.x;
		component.bounds.y -= bounds.y;
	}

	@Override
	public void paint(Graphics g) {
		g.setColor(backgroundColor);
		g.fillRect(getX(), getY(), getWidth(), getHeight());
		g.setColor(outlineColor);
		g.drawRect(getX(), getY(), getWidth(), getHeight());

		for (PaintComponent component : components) {
			component.paint(g);
		}
	}

	@Override
	public int getWidth() {
		if (components.size() > 0) {
			int distance = 0;
			for (PaintComponent component : components) {
				int xAndWidth = component.getX() - bounds.x + component.getWidth();

				if (xAndWidth > distance) {
					distance = xAndWidth;
				}
			}

			return distance + widthOffset;
		}

		return super.getWidth() + widthOffset;
	}

	@Override
	public void mousePressed(MouseEvent e) {
		for (PaintComponent component : components) {
			if (component.getBounds().contains(e.getX(), e.getY())) {
				component.mousePressed(e);
			}
		}
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		for (PaintComponent component : components) {
			if (component.getBounds().contains(e.getX(), e.getY())) {
				component.mouseReleased(e);
			}
		}
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		for (PaintComponent component : components) {
			if (component.getBounds().contains(e.getX(), e.getY())) {
				component.mouseClicked(e);
			}
		}
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		for (PaintComponent component : components) {
			if (component.getBounds().contains(e.getX(), e.getY())) {
				component.mouseEntered(e);
			}
		}
	}

	@Override
	public void mouseExited(MouseEvent e) {
		for (PaintComponent component : components) {
			if (component.getBounds().contains(e.getX(), e.getY())) {
				component.mouseExited(e);
			}
		}
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		for (PaintComponent component : components) {
			if (component.getBounds().contains(e.getX(), e.getY())) {
				component.mouseDragged(e);
			}
		}
	}

}
